import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrl = CommonConfig.fileUrl + '/v2'
const baseUrlTpm = CommonConfig.tpmUrl + '/v1'
// minio 兼容老盒子接口转换
const minioUrl = CommonConfig.fileUrl + '/v1/minio'
/**
 * 获取实际使用大小
 * path 文件路径
 * **/
export function getUseSpaceSize (path) {
  const newUrl = minioUrl + '/cloud_space_usage_size'
  const params = {
    path
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}
// 根据关键字搜索文件
export function findFile (keyword) {
  const newUrl = minioUrl + '/search'
  const params = {
    keyword
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 获取云端文件列表
 * path 文件路径
 * **/
export function getCloudList (path) {
  const newUrl = minioUrl + '/cloud_list'
  const params = {
    path
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 不分权限的获取云端文件列表
 * path 文件路径
 * **/
export function getAllCloudList (path) {
  const newUrl = baseUrl + '/cloud_list/all_permissions'
  const params = {
    path
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据文件夹路径，获取文件夹下的所有文件和文件夹
 * path 文件路径
 * **/
export function getChildPath (path) {
  const newUrl = minioUrl + '/cloud_list_file'
  const params = {
    path
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 删除文件到回收站
 * key 删除路径
 * size 文件大小
 **/
export function rmFileToRecyclebin (params) {
  const newUrl = minioUrl + '/cloud_file_to_recyclebin'

  return axiosInstance.delete(newUrl, { params: params }).then((res) => {
    return Promise.resolve((res))
  })
}
export function rmFileAll (params) {
  const newUrl = minioUrl + '/cloud_to_recyclebin:batch'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// POST
// /v1/minio/cloud_folder_zip
// 文件夹压缩为zip存放到临时路径

export function setFileToZip ({
  folderPath, requestId
}) {
  const newUrl = CommonConfig.fileUrl + '/v1' + '/minio/cloud_folder_zip'
  const params = {
    folderPath, requestId
  }
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取回收站文件列表
 * **/
export function getRecyclebinList () {
  const newUrl = minioUrl + '/cloud_list_personal_recyclebin'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 还原回收站内容
 * key id
 * realname 原路径
 **/
export function restoreRecyclebin (params) {
  const newUrl = minioUrl + '/cloud_file_resume'

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 清空回收站
 * path 删除路径
 * size 文件大小
 **/
export function cleanRecyclebin (params) {
  const newUrl = minioUrl + '/cloud_clean_recyclebin'
  return axiosInstance.delete(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 删除回收站文件
 * key 删除路径
 **/
export function rmFile (key) {
  const newUrl = minioUrl + '/cloud_delete'
  const params = {
    key
  }

  return axiosInstance.delete(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 获取云端文件列表
 * pageSize 获取多少条，默认20
 * **/
export function getLastUseList (pageSize = 20) {
  const newUrl = minioUrl + '/cloud_recently_used'
  const params = {
    pageSize
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取设备台帐列表数据
 * deviceNameOrNumber 设备名称或编号
 * ifBuildBox 是否只显示绑定盒子的设备，默认false
 * schemeId 组织 ID
 * schemeLevel 组织层级
 * pageSize 没页条数
 * pageNum 当前页数
 * searchBy 搜索依据：manufacturer(设备类型), technician（数控系统）, number, name, model
 * keyword 搜索关键字
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getEquipmentAccount (params) {
  const newUrl = `${baseUrlTpm}/devices`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}
